package net.uzdev;

import java.io.File;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

public class ServerStarter {
    public static void main(String[] args) throws LifecycleException{
        
        ServerStarter.startServer();
    }

    public static void startServer() throws LifecycleException{
        Tomcat tomcat = new Tomcat();
        tomcat.getConnector();
        // 设置webapp目录
        Context context =  tomcat.addWebapp("", new File("webapp").getAbsolutePath());
        System.out.println(context);
        // web项目路径
        WebResourceRoot resources = new StandardRoot(context);
        // addPreResources 指向servlet 或 controller所在目录
        resources.addPreResources(
                new DirResourceSet(resources, "/WEB-INF/classes",
                        new File("target/classes").getAbsolutePath(), "/"));     // target/classes 为存放servlet类的目录
        context.setResources(resources);
        tomcat.start();
        tomcat.getServer().await();
        System.out.println("Tomcat 服务器已启动...");
    }
}
